package com.ym.b2b.tags;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.ym.b2b.commons.conf.B2BConstants;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.product.service.ProductService;
import com.ym.b2b.product.sqlbean.EmBProduct;
import com.ym.b2b.tags.base.BaseTag;
import com.zc.commons.base.Result;
import com.zc.commons.spring.SpringContextHolder;

/**
 * @ClassName: ProductListTags
 * @Description: 获取商品 简易列表标签
 * @Author 竺夏军
 * @Date 2015年1月28日 上午12:02:33
 * @Modify
 * @CopyRight 信息化团队
 */
public class ProductListTags extends BaseTag {

    private Integer           isIndex;
    private Integer           isTop;		//	1 首页推荐
    private Integer           isCommend;	//	1.推荐品种
    private Integer           limitRow;
    protected Integer         isNew;
    private String            productTypeCode;                       // 获取该分类编码下的商品
    protected Integer         isComment;
    private Long              brandId;

    private static final long serialVersionUID = 895777383599438520L;

    public int doStartTag() {
        Result ret = new Result();
        String cacheKey = "key_";
        EmBProduct emBProduct = new EmBProduct();
        emBProduct.setSearchFlag("tag");
        emBProduct.setApproveStatus(10);
        emBProduct.setIsSale(1);

        emBProduct.setLimitRow(getLimitRow());
        cacheKey += "row:" + getLimitRow();

        emBProduct.setIsIndex(getIsIndex());
        if (null != getIsIndex()) {
            cacheKey += "index:" + getIsIndex();
        }
        emBProduct.setIsTop(getIsTop());
        if (null != getIsTop()) {
            cacheKey += "top:" + getIsTop();
        }
        emBProduct.setIsCommend(getIsCommend());

        if (null != getIsCommend()) {
            cacheKey += "cmd:" + getIsCommend();
        }
/*        emBProduct.setIsNew(getIsNew());
        if (null != getIsNew()) {
            cacheKey += "new:" + getIsNew();
        }*/
		if (null != getIsNew() && getIsNew()==1) {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
			Calendar lastDate = Calendar.getInstance();  
			lastDate.add(Calendar.MONTH,-6);//减一个月，变为下月的1号  
			emBProduct.setDenglrq(sdf.format(lastDate.getTime()));
			emBProduct.setOrderByType("NEW_PRODUCT");
		}
        emBProduct.setIsComment(isComment);
        if (null != isComment) {
            cacheKey += "cmt:" + isComment;
        }
        emBProduct.setBrandId(getBrandId());
        if (null != getBrandId()) {
            cacheKey += "bd:" + getBrandId();
        }


        try {
            WebTokenUser a = (WebTokenUser) getPageContext().getSession().getAttribute(B2BConstants.WEB_USER_TOKEN);
            if (null != a) {
                emBProduct.setUser(a);
                cacheKey += "buyerCode:" + a.getBuyerCode();
            }
        } catch (Exception e) {

        }
        emBProduct.setCacheKey(cacheKey);
        ProductService productService = SpringContextHolder.getBean("productService");
        ret = productService.searchSimpleProductList(emBProduct);
        getPageContext().getRequest().setAttribute(getOutResponseAttrName(), ret);
        return EVAL_BODY_INCLUDE;
    }

    public Integer getIsIndex() {
        return isIndex;
    }

    public void setIsIndex(Integer isIndex) {
        this.isIndex = isIndex;
    }

    public Integer getIsTop() {
        return isTop;
    }

    public void setIsTop(Integer isTop) {
        this.isTop = isTop;
    }

    public Integer getIsCommend() {
        return isCommend;
    }

    public void setIsCommend(Integer isCommend) {
        this.isCommend = isCommend;
    }

    public Integer getLimitRow() {
        return limitRow;
    }

    public void setLimitRow(Integer limitRow) {
        this.limitRow = limitRow;
    }

    public Integer getIsNew() {
        return isNew;
    }

    public void setIsNew(Integer isNew) {
        this.isNew = isNew;
    }

    public String getProductTypeCode() {
        return productTypeCode;
    }

    public void setProductTypeCode(String productTypeCode) {
        this.productTypeCode = productTypeCode;
    }

    public void setIsComment(Integer isComment) {
        this.isComment = isComment;
    }

    public Long getBrandId() {
        return brandId;
    }

    public void setBrandId(Long brandId) {
        this.brandId = brandId;
    }

}
